#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re

from playwright.sync_api import Page
import allure
from dragon.selector import Selector
from dragon.should import PageShould
from dragon.mouse import Mouse
from dragon.locator import DraLocator,DraFrameLocator
import faker
import logging
from typing import Callable,Pattern,Literal


class DraPage:
    def __init__(self, page: Page,env):
        self._page = page
        self._page.set_default_timeout(10000)
        self.env = env
        self.mouse=Mouse(self)
        self.fake = faker.Faker('zh_CN')
        # self.request=page.request

    def on_load(self,func:Callable):
        return self._page.on('load',func)

    def on_dialog(self,func:Callable):
        return self._page.on('dialog',func)

    def on_request(self,func):
        return self._page.on('request',func)

    def once_load(self,func:Callable):
        self._page.once('load',func)

    def frame_locator(self,se:Selector):
        return DraFrameLocator(self._page.frame_locator(se.selector), se, self)

    def locator(self,se:Selector):
        # selector=self._page.selector(se.selector)
        # selector.last.wait_for()
        locator=self._page.locator(se.selector)
        # selector.highlight()
        return DraLocator(locator,se,self)

    def reload(self,wait_until:Literal["commit", "domcontentloaded", "load", "networkidle"]='load'):
        stepText = '步骤：刷新页面'
        with allure.step(stepText):
            logging.info(stepText)
            self._page.reload(wait_until=wait_until)
            return self

    def goto(self, url: str,wait_until:Literal["commit", "domcontentloaded", "load", "networkidle"]='load'):
        stepText = f'步骤：访问url：【{url}】'
        with allure.step(stepText):
            logging.info(stepText)
            self._page.goto(url,wait_until=wait_until)
            return self

    def go_back(self,wait_until:Literal["commit", "domcontentloaded", "load", "networkidle"]='load'):
        stepText = f'步骤：后退一页'
        with allure.step(stepText):
            logging.info(stepText)
            self._page.go_back(wait_until=wait_until)
        return self

    def go_forward(self,wait_until:Literal["commit", "domcontentloaded", "load", "networkidle"]='load'):
        stepText = f'步骤：前进一页'
        with allure.step(stepText):
            logging.info(stepText)
            self._page.go_forward(wait_until=wait_until)
        return self

    def wait_for_timeout(self,timeout=1000):
        self._page.wait_for_timeout(timeout)

    def wait_for_url(self,text:str|Pattern, timeout=1000):
        text=text.replace('?',r'\?')
        self._page.wait_for_url(re.compile(text),timeout=timeout)

    def wait_for_load_state(self,state:Literal["domcontentloaded", "load", "networkidle"]='load',timeout=10000):
        self._page.wait_for_load_state(state,timeout=timeout)

    def pause(self):
        self._page.pause()

    def should(self):
        return PageShould(self)

    def execute_script(self, js: str) -> any:
        return self._page.evaluate(js)

    def screenshot(self, path=None,type:Literal['png','jpeg']='png',full_page:bool=False):
        return self._page.screenshot(path=path,type=type,full_page=full_page)

    def bring_to_front(self):
        self._page.bring_to_front()
        return self

    def get_dra_page(self,index):
        pages = self.context.pages
        if index>=0:
            stepText=f'步骤：获取第【{index+1}】个页面'
        else:
            stepText= f'倒数第{abs(index)}个页面'
        with allure.step(stepText):
            logging.info(stepText)
            draPage=DraPage(pages[index],self.env)
        return draPage

    @property
    def title(self):
        return self._page.title()

    @property
    def url(self):
        return self._page.url

    @property
    def context(self):
        return self._page.context

    @property
    def request(self):
        return self._page.request
